package com.jyh.service.money;


import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.db.entity.JyhPlatformAccount;
import com.jyh.db.entity.JyhPlatformAccountExample;
import com.jyh.db.mapper.JyhPlatformAccountMapper;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.util.StringUtils;

@Service
public class AccountSystemService extends BaseAction {
	@Autowired
	private LoanDao loanDao; 
	@Autowired
	private JyhPlatformAccountMapper accountMapper;
	/**
	 * 获取账户体系页面数据
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getAccountSystem(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("money/accountSystem");
		String userNO  = request.getParameter("userNO");
		String userName  = request.getParameter("userName");
		String loginAccount  = request.getParameter("loginAccount");
		
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_user_account  where 1=1";
		String sql = "select * from jyh_user_account  where 1=1";
		if (StringUtils.isNotNull(loginAccount)) {
			sql = sql + " and loginAccount='" + loginAccount + "'";
			sql1 = sql1 + " and loginAccount='" + loginAccount + "'";
		}
		if (StringUtils.isNotNull(userNO)) {
			sql = sql + " and userNO='" + userNO + "'";
			sql1 = sql1 + " and userNO='" + userNO + "'";
		}
		if (StringUtils.isNotNull(userName)) {
			sql = sql + " and userName='" + userName + "'";
			sql1 = sql1 + " and userName='" + userName + "'";
		}
		
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		List<JyhPlatformAccount> accountList = accountMapper.selectByExample(new JyhPlatformAccountExample());
		
		mv.addObject("list", list);
		mv.addObject("map", accountList.get(0));
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		//将传递过来的参数回显
		mv.addObject("userNO", userNO);
		mv.addObject("userName", userName);
		mv.addObject("loginAccount", loginAccount);
		return mv;
	}
	
}
